package com.qf.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.qf.feigns.OrderServiceApi;
import com.qf.pojo.R;
import com.qf.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Enumeration;

@RestController
@RequestMapping("/user")
@RefreshScope //这个注解加上后，一旦配置信息发生变化，会重建该对象
public class UserController4 {

    @Value("${desc}")
    private String desc;

    @Autowired
    private OrderServiceApi orderServiceApi;

    @Autowired
    private UserService userService;

    @GetMapping("/{id}")
    @SentinelResource(value = "findOrders")
    public R findOrders(@PathVariable("id") Integer id,HttpServletRequest req) throws InterruptedException {

        System.out.println("--------------------------");
        Enumeration<String> headerNames = req.getHeaderNames();

        while (headerNames.hasMoreElements()){
            String headName = headerNames.nextElement();
            String headerValue = req.getHeader(headName);

            System.out.println(headName + "==" + headerValue);
        }

        System.out.println("---------------------------");

        System.out.println("===========desc:" + desc);

        R r = orderServiceApi.findByUid(id);

        return r;
    }

    @GetMapping("/submit")
    @SentinelResource("submitOrder")
    public R submitOrder(){
        System.out.println("下单...");

        userService.getCarts();

        return R.success().putMsg("下单成功...");
    }

    @GetMapping("/carts")
    @SentinelResource("carts")
    public R carts(){
        userService.getCarts();
        return R.success().putMsg("查询购物车成功...");
    }


}
